import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


function makeNewConfigCurrentSheet(data, config) {
  if (data == null) {
    return config
  }
  let currentSheet = ''
  if (data.sheetList.length > 0) {
    currentSheet = data.sheetList[0].id
  }
  return Object.assign({}, config, {
    currentSheet,
    nameColumn: '',
    numberColumn: {},
  })
}

export default new Vuex.Store({
  state: {
    tableDataA: null,
    tableDataB: null,
    configDataA: {
      currentSheet: '',
      nameColumn: '',
      nameTrim: false,
      numberColumn: {},
    },
    configDataB: {
      currentSheet: '',
      nameColumn: '',
      nameTrim: false,
      numberColumn: {},
    },
    // cmp result
    result: null,
  },
  mutations: {
    setDataA(state, data) {
      state.tableDataA = data
    },
    setDataB(state, data) {
      state.tableDataB = data
    },
    setConfigA(state, data) {
      state.configDataA = data
    },
    setConfigB(state, data) {
      state.configDataB = data
    },
    setResult(state, data) {
      state.result = data
    },
  },
  actions: {
    updateDataA({ commit, state }, data) {
      commit('setDataA', data)
      commit('setConfigA', makeNewConfigCurrentSheet(data, state.configDataA))
    },
    updateDataB({ commit, state }, data) {
      commit('setDataB', data)
      commit('setConfigB', makeNewConfigCurrentSheet(data, state.configDataB))
    },
  },
})
